﻿Imports DTO
Imports System.Data.SqlClient
Public Class CDauSachDAO
    Public Function LayDanhSachDauSach() As List(Of CDauSachDTO)
        Using cn As SqlConnection = CDataProvider.Connect()
            Dim cm As New SqlCommand("uspLayDanhSachDauSach", cn)
            cm.CommandType = CommandType.StoredProcedure
            Dim ldausach As New List(Of CDauSachDTO)()
            Using reader As IDataReader = cm.ExecuteReader(CommandBehavior.CloseConnection)
                While reader.Read()
                    ldausach.Add(New CDauSachDTO(reader))
                End While
            End Using
            cn.Close()
            Return ldausach
        End Using
    End Function

    Public Function ThemDauSach(ByVal ds As CDauSachDTO) As Integer
        Using cn As SqlConnection = CDataProvider.Connect()
            Dim cm As New SqlCommand("uspThemDauSach", cn)
            cm.CommandType = CommandType.StoredProcedure
            Dim ldausach As New List(Of CDauSachDTO)()
            Dim para As SqlParameter = New SqlParameter("@ten", ds.Ten)
            cm.Parameters.Add(para)
            para = New SqlParameter("@manguoitiepnhan", ds.MaNhanVien)
            cm.Parameters.Add(para)
            para = New SqlParameter("@matheloai", ds.MaTheLoai)
            cm.Parameters.Add(para)
            para = New SqlParameter("@matinhtrang", ds.MaTinhTrang)
            cm.Parameters.Add(para)
            para = New SqlParameter("@namxuatban", ds.NamXuatBan)
            cm.Parameters.Add(para)
            para = New SqlParameter("@ngaynhap", ds.NgayNhap)
            cm.Parameters.Add(para)
            para = New SqlParameter("@nhaxuatban", ds.NhaXuatBan)
            cm.Parameters.Add(para)
            para = New SqlParameter("@tienphatmatsach", ds.TienPhatMatSach)
            cm.Parameters.Add(para)
            para = New SqlParameter("@tongsosach", ds.TongSoSach)
            cm.Parameters.Add(para)
            para = New SqlParameter("@trigia", ds.TriGia)
            cm.Parameters.Add(para)
            para = New SqlParameter("@tentacgia", ds.TacGia)
            cm.Parameters.Add(para)
            para = New SqlParameter("@ma", 0)
            para.Direction = ParameterDirection.Output
            cm.Parameters.Add(para)
            Dim n As Integer = cm.ExecuteNonQuery()
            ds.Ma = cm.Parameters("@ma").Value
            For i As Integer = 0 To ds.TongSoSach - 1
                cm = New SqlCommand("uspThemSach", cn)
                cm.CommandType = CommandType.StoredProcedure
                para = New SqlParameter("@madausach", ds.Ma)
                cm.Parameters.Add(para)
                cm.ExecuteNonQuery()
            Next
            cn.Close()
            Return n
        End Using
    End Function

    Public Function CapNhatDauSach(ByVal ds As CDauSachDTO) As Integer
        Using cn As SqlConnection = CDataProvider.Connect()
            Dim cm As New SqlCommand("uspCapNhatDauSach", cn)
            cm.CommandType = CommandType.StoredProcedure
            Dim ldausach As New List(Of CDauSachDTO)()
            Dim para As SqlParameter = New SqlParameter("@ten", ds.Ten)
            cm.Parameters.Add(para)
            para = New SqlParameter("@manguoitiepnhan", ds.MaNhanVien)
            cm.Parameters.Add(para)
            para = New SqlParameter("@matheloai", ds.MaTheLoai)
            cm.Parameters.Add(para)
            para = New SqlParameter("@matinhtrang", ds.MaTinhTrang)
            cm.Parameters.Add(para)
            para = New SqlParameter("@namxuatban", ds.NamXuatBan)
            cm.Parameters.Add(para)
            para = New SqlParameter("@ngaynhap", ds.NgayNhap)
            cm.Parameters.Add(para)
            para = New SqlParameter("@nhaxuatban", ds.NhaXuatBan)
            cm.Parameters.Add(para)
            para = New SqlParameter("@tienphatmatsach", ds.TienPhatMatSach)
            cm.Parameters.Add(para)
            para = New SqlParameter("@tongsosach", ds.TongSoSach)
            cm.Parameters.Add(para)
            para = New SqlParameter("@trigia", ds.TriGia)
            cm.Parameters.Add(para)
            para = New SqlParameter("@tentacgia", ds.TacGia)
            cm.Parameters.Add(para)
            para = New SqlParameter("@ma", ds.Ma)
            cm.Parameters.Add(para)
            Dim n As Integer = cm.ExecuteNonQuery()
            cn.Close()
            Return n
        End Using
    End Function
End Class
